using System;
using System.Management;
using System.Diagnostics;

using System.Security;
using System.Security.Principal;

namespace CSharpDistilled.Diagnostics
{
    /// <summary>
    /// Represents the operating system.
    /// </summary>
    [Serializable]
    public sealed class OperatingSystem : Device
    {
        #region public OperatingSystem()

        /// <summary>
        /// Initializes a new instance of the <see cref="OperatingSystem"/> class.
        /// </summary>
        public OperatingSystem()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_OperatingSystem");

                foreach (ManagementObject managementObject in searcher.Get())
                {
                    string name = managementObject["Name"] as string;

                    this.Name = name.Substring(0, name.IndexOf('|'));
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);

                throw;
            }
        }

        #endregion
    }
}
